package com.kclm.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author : lvhx
 * @Date: 2025/10/28 14:06
 * @Description:
 * 封装一个获取SqlSession的工具类
 */
public class SqlSessionUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);

        }catch (Exception e){
            throw  new RuntimeException("初始化mybatis失败");
        }
    }
    // 获取SqlSession的惊天方法
    public static SqlSession  getSqlSession(){
        return sqlSessionFactory.openSession();
    }



}
